package com.atguigu.order.service.Impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.order.domain.Order;
import com.atguigu.order.feign.ProductFeignClient;
import com.atguigu.order.service.OrderService;
import com.atguigu.product.domain.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductFeignClient productFeignClient;

    @SentinelResource("generate")
    @Override
    public Order generate(Long userId, Long productId) {
        // 远程调用
//        Product product = getProduct(productId);
        // 通过负载均衡获取
//        Product product = getProductByBalance(productId);
        // 通过注解负载均衡获取
//        Product product = getProductByBalanceAndAnnotation(productId);
        // 用feign来调用
        Product product = productFeignClient.getProductById(productId);

        Order order = new Order();
        order.setId(1L);
        order.setTotalPrice(product.getPrice().multiply(BigDecimal.valueOf(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zlm");
        order.setAddress("总部");
        order.setProductList(null);
        System.out.println("订单生成成功");
        order.setProductList(Arrays.asList(product));

        return order;
    }

    public Product getProduct(Long productId) {
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);
        StringBuffer productUrl = new StringBuffer();
        productUrl.append("http://");
        productUrl.append(instance.getHost());
        productUrl.append(":");
        productUrl.append(instance.getPort());
        productUrl.append("/product/");
        productUrl.append(productId);

        log.info("productUrl:{}", productUrl);
        Product product = restTemplate.getForObject(productUrl.toString(), Product.class);
        return product;
    }

    /**
     * 通过负载均衡获取
     *
     * @param productId
     * @return
     */
    public Product getProductByBalance(Long productId) {
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        StringBuffer productUrl = new StringBuffer();
        productUrl.append(choose.getUri());
        productUrl.append("/product/");
        productUrl.append(productId);

        log.info("productUrl:{}", productUrl);
        Product product = restTemplate.getForObject(productUrl.toString(), Product.class);
        return product;
    }

    /**
     * 通过注解负载均衡获取
     *
     * @param productId
     * @return
     */
    public Product getProductByBalanceAndAnnotation(Long productId) {
        StringBuffer productUrl = new StringBuffer();
        productUrl.append("http://");
        // 服务名称
        productUrl.append("service-product");
        productUrl.append("/product/");
        productUrl.append(productId);

        log.info("productUrl:{}", productUrl);
        Product product = restTemplate.getForObject(productUrl.toString(), Product.class);
        return product;
    }

    @Override
    public void modify() {
        System.out.println("订单修改成功");
    }

    @Override
    public void detail() {
        System.out.println("订单详情");
    }

    @Override
    public void pay() {
        System.out.println("订单支付成功");
    }
}
